#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#define _WINSOCKAPI_
#include <QtWidgets/QMainWindow>
#include "ui_mainwindow.h"
//#include "cfgdialog.h"
#include "imagemodel.h"
#include "serialport.h"
#include<QPainter>
#include<QMovie>
#include <ActiveQt/QAxBase> 
#include <ActiveQt/QAxWidget>
#include <ActiveQt/QAxObject>

#include <QTcpSocket>

#define Q_SERVER_PORT 9000
#define Q_SERVER_ADDR "127.0.0.1"
//#define Q_SERVER_ADDR "192.168.1.101"

class MainWindow : public QMainWindow
{
	Q_OBJECT

public:
	MainWindow(QWidget *parent = 0);
	~MainWindow();

	bool initCameraConfig();
	bool saveCameraConfig();
	void setGain();
	int getGainData(int gain);

	int initSock();
	int uninitSock();
	int sockWrite(const char *data);


public slots:

	void onSockConnected();
	void onSockReadread();
	void onSockError(QAbstractSocket::SocketError socketError);
	void onSockState(QAbstractSocket::SocketState socketState);

	void openCamera();
	void closeCamera();
	void pauseCamera();

	void updateImage(QPixmap image);

	void showFrameRate();

	void saveData();

	void countReceiveFrames();

	void changeWidthTo8bitsPerPixel(bool flag);
	
	void changeImageToColor(bool flag);

	//void switchResolution(int index);
	void switchResolution_1280(bool);
	void switchResolution_640(bool);
	void switchResolution_320(bool);
	void switchResolution_1920(bool);
	void switchResolution_2048(bool);
	void switchResolution_2592(bool);
	void switchResolution_rtsp(int height, int width);
	void setAnalogGain_1(bool);
	void setAnalogGain_2(bool);
	void setAnalogGain_4(bool);
	void setAnalogGain_8(bool);
	void setAnalogGain_16(bool);

	//void setAnalogGain(int index);
	
	//void setRedGain(int gain);
	//void setGreenGain(int gain);
	//void setBlueGain(int gain);
	//void setGlobalGain(int gain);
	void setRedGain_1(bool);
	void setRedGain_2(bool);
	void setRedGain_4(bool);
	void setRedGain_8(bool);
	void setRedGain_16(bool);
	void setGreenGain_1(bool);
	void setGreenGain_2(bool);
	void setGreenGain_4(bool);
	void setGreenGain_8(bool);
	void setGreenGain_16(bool);
	void setBlueGain_1(bool);
	void setBlueGain_2(bool);
	void setBlueGain_4(bool);
	void setBlueGain_8(bool);
	void setBlueGain_16(bool);
	void setGlobalGain_1(bool);
	void setGlobalGain_2(bool);
	void setGlobalGain_4(bool);
	void setGlobalGain_8(bool);
	void setGlobalGain_16(bool);

	void setExposureMode(bool isAuto);
	void setExposureValue(int value);
	void chooseSavingPath();
	void openCfgDialog();
	void takeImage();
	void showCurve();
	void openImage();
	void setHorizontalLine();
	void setVerticalLine();
	void setInputDataLine();
	//void configMyGraphPlot();
	//void showAnalysePoint();
	void getPointData(int, int, int);
	void clearPointData();
	void startCountTime(int, int, int, int, int, int);
	void setPointChoosed_o();
	void setPointChoosed_a();
	void setPointChoosed_b();
	void setPointChoosed_c();
	void setPointChoosed_d();
	void setPointChoosed_e();
	void setPointChoosed_f();
	void getPointInput();
	void countPRNU();
	void countDSNU();
	void takeDarkImage();
	void takeLightImage();
	void calNonuniformity();
	void setNonuniformityEnable();
	void showPRNU(QString);
	void wrongTipsOfPRNU(bool);
	void showDSNU(QString);
	void wrongTipsOfDSNU(bool);
	//void countTimeVariance();
//	void countSNR();
	//void countDarkCurrent();
	//void showTimeVariance(QString);
//	void showSNR(QString);
	void updateGraph();
	void updatePointData();
//	void countEfficiencyData();
//	void countImageGray();
	//void countEfficiency();
	void showParameter(QString, QString, QString, QString, QString);
//	void showImageGray(QString);
//	void countR();
	void luminanceWrongTip();
	/*void countDarkTimeVariance();*/
	void showDarkTimeVariance(QString);
	void showDarkCurrerntWrongTip();
	void showDarkCurrent(QString);
	void calGrayScale();
	void showGrayScale(QString);
	void calMTF();
	void spectralResponse();
	void photoelectricResponse();
	void showSpectralResponse(QVector<int>, QVector<float>);
	void showPhotoelectricResponse(QVector<float>, QVector<int>);
	void getImageGray(int);
	void getTimeVariance(float);
	void countImageGray();
	void countTimeVariance();
	void showLimitResolution(QString);
	void calDistortion();
	void showDistortion(QString);
	void reportData();
	void exportCoordinateData(QString path, QVector<double> &x, QVector<double> &y, QString xName, QString yName);
	void savePhotoelectricResponse(QString);
	void saveSpectralResponse(QString);
	void saveMTF(QString);
	void getAnalyzeImage(QString, QString,int);
	void takeAnalyzeImage();
	void showMeasureFailer();
	void getSockData();
	void initCorrect();
	void closeLight();

public:
	ImageModel m_imageModel;

private:
	QTcpSocket *clntSock;
	Ui::MainWindowClass ui;

	SerialPort m_serialPort;
	
	QTimer m_timer;
	QTimer sock_timer;

	QLabel *m_frameRateLabel;

	QString path;

	int m_frameCount;

	int m_receiveFramesCount;

	int m_imageWidth;
	int m_imageHeight;

	bool m_isClosed;
	int showImageWidth ;
	int showImageHeight;
	int openImageWidth;
	int openImageHeight;

	double m_rtspShowMultiple;
	int m_rtspShowImageWidth;
	int m_rtspShowImageHeight;

//	bool openImageFlag;
	int resolutionType;

	QPixmap image_show;
	bool bits_8_flag; 
	int analogGain;
	int redGain;
	int greenGain;
	int blueGain;
	bool getPoint;
	int updataGraphFlag;
	bool openCameraFlag;
	QTime startTime;
	int y_orig_fake_m;

//	QMessageBox messageBox;
//	bool takingImageFlag;
	QVector<int> gray;
	QVector<int> data_arr;
	QVector<int> msecs_arr;
	QTimer graph_timer;
	QTimer pointData_timer;
	QVector<float> luminanceArray;
	QVector<float> exposureValueArray;
	int exposureValue;
	QVector<float> exposureValueArray2;
	bool showRealTimeDataFlag;
	QMovie *movie;

	QString spectralImagePath;
	QString photoelectricImagePath;
	QString PRNU;
	QString DSNU;
	QString noise;
	QString SNR; 
	QString DR;
	QString MTFPath;
	QString limitResolution;
	QString grayScale;
	QString MRC;
	QString distortion;
	int analyzeType;
	int sockValue;
	int statValue;
	int sockCmd;
	int sockRequestCount;
	int measureType;
};


#endif // MAINWINDOW_H
